package org.examples;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;

import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.logging.LogLevel;
import lombok.extern.slf4j.Slf4j;
import reactor.netty.http.client.HttpClient;
import reactor.netty.transport.logging.AdvancedByteBufFormat;

import static org.examples.DebugConstants.host;
import static org.examples.DebugConstants.vocsInputJson;
import static org.examples.DebugConstants.isHttps;
import static org.examples.DebugConstants.vocsParametersJson;
import static org.examples.DebugConstants.vocsPath;

@Slf4j
public class ReactorVocs {
  public static void main(final String[] args) throws Exception {
    log.info("ReactorNtts.main");
    //reactor.netty.http.client.HttpClient
    HttpClient client = HttpClient.create()
        .wiretap("reactor.netty.http.client.HttpClient", LogLevel.DEBUG, AdvancedByteBufFormat.HEX_DUMP)
//        .wiretap(true)
        .baseUrl(host);
    if (isHttps()) {
      client = client.secure();
    }
    client
        //        .headers(headers -> {
        //          headers.set(HttpHeaderNames.ACCEPT, "audio/ogg;codecs=opus");
        //        })
        .post()               // Specifies that POST method will be used
        .uri(vocsPath())   // Specifies the path
        .sendForm((req, form) -> {
          req.requestHeaders().remove(HttpHeaderNames.TRANSFER_ENCODING);
          form
              .multipart(true)
                            .file("Input", new ByteArrayInputStream(vocsInputJson.getBytes(StandardCharsets.UTF_8)), "application/json")
                            .file("Parameters", new ByteArrayInputStream(vocsParametersJson.getBytes(StandardCharsets.UTF_8)), "application/json");
//              .attr("Input", "inputJson")
//              .attr("Parameters", "parametersJson");
        })  // Sends the request body

        .responseContent()    // Receives the response body
//                        .aggregate()
//                        .asString()
//        .log("http-client")
//        .log("HttpClient")
        .log()
//                .block();
        .blockLast();
  }
}
